﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interface;
using Microhard.Core;
using Microhard.Core.DBUtility;
using Microhard.Plat.CompanyChargingMng.DataAccess;
using Microhard.Plat.CompanyChargingMng.Interfaces;


namespace Microhard.Plat.CompanyChargingMng.Impls
{
    public class FMCGWXAccountValidator : IWeiXinAccount
    {
        public bool ValidateByWeiXinUserName(string weixinUserName, out string userName)
        {
            userName = string.Empty;
            Microhard_ChargingEntities db = new Microhard_ChargingEntities();
            //是否是员工
            var cnt = db.Employees.SingleOrDefault(e => e.WeixinOpenID == weixinUserName);
            if (cnt != null)
            {
                if (!string.IsNullOrEmpty(cnt.UserName))
                {
                    userName = cnt.UserName;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 绑定员工账户信息
        /// </summary>
        /// <param name="weixinUserName"></param>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public Microhard.Core.DBUtility.ExecutedResult BindingWeixinAccount(string weixinUserName, string userName, string pwd)
        {
            ExecutedResult result = new ExecutedResult();
            IUser user = IoC.Resolve<IUser>();
            if (user.ValidatUser(userName, pwd))
            {
                IEmployee emp = IoC.Resolve<IEmployee>();
                result = emp.UpdateUserWeixinOpenID(userName, weixinUserName);
            }
            else
            {
                result.ErrorMsg.Add("userName", "用户名密码错误");
            }
            return result;
        }
    }
}
